package com.zonda.rxjavaexample.apps;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zonda.rxjavaexample.R;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.ViewHolder> {

  private List<AppInfo> mApplications;

  private int mRowLayout;

  public ApplicationAdapter(List<AppInfo> applications, int rowLayout) {
    mApplications = applications;
    mRowLayout = rowLayout;
  }

  public void addApplications(List<AppInfo> applications) {
    mApplications.clear();
    mApplications.addAll(applications);
    notifyDataSetChanged();
  }

  public void addApplication(int position, AppInfo appInfo) {
    if (position < 0) {
      position = 0;
    }
    mApplications.add(position, appInfo);
    notifyItemInserted(position);
  }

  @Override
  public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(mRowLayout, viewGroup, false);
    return new ViewHolder(v);
  }

  @Override
  public void onBindViewHolder(final ViewHolder viewHolder, int i) {
    final AppInfo appInfo = mApplications.get(i);
    viewHolder.name.setText(appInfo.getName());
    getBitmap(appInfo.getIcon()).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<Bitmap>() {
          @Override
          public void call(Bitmap bitmap) {
//            viewHolder.image::setImageBitmap
            viewHolder.image.setImageBitmap(bitmap);
          }
        });
  }

  @Override
  public int getItemCount() {
    return mApplications == null ? 0 : mApplications.size();
  }

  private Observable<Bitmap> getBitmap(final String icon) {

    return Observable.create(new Observable.OnSubscribe<Bitmap>() {
      @Override
      public void call(Subscriber<? super Bitmap> subscriber) {
        subscriber.onNext(BitmapFactory.decodeFile(icon));
        subscriber.onCompleted();
      }
    });
//    return Observable.create(subscriber -> {
//      subscriber.onNext(BitmapFactory.decodeFile(icon));
//      subscriber.onCompleted();
//    });
  }

  public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView name;

    public ImageView image;

    public ViewHolder(View itemView) {
      super(itemView);
      name = (TextView) itemView.findViewById(R.id.name);
      image = (ImageView) itemView.findViewById(R.id.image);
    }
  }
}
